<?php

namespace Console\Logic;

use \Common\Logic\BaseLogic;

class RoleLogic extends BaseLogic
{

    //免验证，例如框架页面
    static $filter = array(
        "index_index" => 1
    );

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 验证是否有访问权限
     * @param $role
     * @param $controller
     * @param $action
     * @return bool
     */
    public function verityAuth($role, $controller, $action)
    {
        $controller = strtolower($controller);
        $action = strtolower($action);
        $key = $controller . '_' . $action;

        //免验证
        if (static::$filter[$key]) {
            return true;
        }

        $infoRes = $this->getRoleInfo($role);
        if (static::$res->isErr($infoRes)) {
            return false;
        }
        $info = $infoRes['Result'];
        $auth = $info['auth'];

        if ($auth[$key]) {
            return true;
        }
        return false;
    }

    public function getMenu($role)
    {
        $infoRes = $this->getRoleInfo($role);
        if (static::$res->isErr($infoRes)) {
            return $infoRes;
        }
        $info = $infoRes['Result'];
        $auth = $info['auth'];

        $menuList = array();
        foreach (C('menuList') as $key => $vo) {
            if ($vo['hide']) {
                continue;
            }
            if (empty($vo['sub'])) {
                if ($auth[$key]) {
                    $vo['sign'] = $key;
                    $menuList[] = $vo;
                }
                continue;
            }

            $sub = $vo['sub'];
            $subList = array();
            foreach ($sub as $subKey => $subVo) {
                if ($subVo['hide']) {
                    continue;
                }
                if ($auth[$subKey]) {
                    $subVo['sign'] = $subKey;
                    $subList[] = $subVo;
                }
            }
            if (!empty($menuList)) {
                $vo['sign'] = $key;
                $vo['sub'] = $subList;
                $menuList[] = $vo;
            }
        }

        if (empty($menuList)) {
            return static::$res->err("没有任何权限");
        }

        return static::$res->suc("成功", $menuList);
    }

    /**
     * 获取管理角色
     * @param $role
     * @return mixed
     */
    public function getRoleInfo($role)
    {
        if (empty($role)) {
            return static::$res->err("角色ID不能为空");
        }

        switch ($role) {
            case 1:
                $roleInfo = array(
                    "name" => "超级管理员",
                    "sign" => "superAdmin",
                    "auth" => C('roleAuth.superAdmin')
                );
                break;
            case 2:
                $roleInfo = array(
                    "name" => "系统管理员",
                    "sign" => "admin",
                    "auth" => C('roleAuth.admin')
                );
                break;
            case 3:
                $roleInfo = array(
                    "name" => "管理员",
                    "sign" => "manage",
                    "auth" => C('roleAuth.manage')
                );
                break;
            default:
                $roleInfo = array();
        }

        if (empty($roleInfo)) {
            return static::$res->err("系统不支持的角色");
        }

        return static::$res->suc("成功", $roleInfo);
    }

}
